<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class SellsideService 
{
	
	public static function process_dadf_request($request) {
		
		$dadf_version = Zend_Registry::get('application.version');
		
	    try {		
    		$request_xml 			= $request->getParam('xml');
    		$request_remotehost 	= $request->getParam('remotehost');
    		$remote_url 			= $request_remotehost . '/buyside/dadfrequest/';
    		$transformer 			= new IncomingRequestTransformer($request_xml, 'Dadf.request-' . $dadf_version . '.xsd', 'Outgoing');
			$bid_request_id 		= $transformer->bid_request_id;
    		$transformer 			= new OutgoingRequestTransformer($bid_request_id, $remote_url, 'Outgoing');
			
	        $data = array(
			    'xml' => $transformer->xmlData
			);
			
			$content = self::curl_post($remote_url, $data, $retcode);
            
			$outgoing_bid_request_host_factory = OutgoingBidRequestHostFactory::get_instance();
			$params = array();
			$params["OutgoingBidRequestID"] = $bid_request_id;
			$bid_request_host = $outgoing_bid_request_host_factory->get_row($params);
            
			if ($bid_request_host != null):
				$bid_request_host->HttpResponseCode = $retcode;
				$bid_request_host->save();
			endif;
			
            if ($retcode != 200):
            	throw new Exception("REST Call to remote server failed");
            endif;
            
            $transformer = new IncomingOfferTransformer($content, 'Dadf.offer-' . $dadf_version . '.xsd', 'Incoming');
     		
            header("Content-type: text/xml");
			echo $content;
			
    	} catch (Exception $e) {
    				
    		if (DEBUG === true):
    			echo 'Caught exception: ',  $e->getMessage(), "\n";
    		endif;
    		
    		echo "Bad Request Response";
    			
    	}
	}
	
	public static function process_dadf_accept($request) {
		
		$dadf_version = Zend_Registry::get('application.version');
		
        try {
    				
    		$request_xml 				= $request->getParam('xml');
    		$request_remotehost 		= $request->getParam('remotehost');
    		$remote_url 				= $request_remotehost . '/buyside/dadfaccept/';
    		$transformer 				= new IncomingAcceptTransformer($request_xml, 'Dadf.accept-' . $dadf_version . '.xsd', 'Outgoing');
    		$accept_response_id_list	= $transformer->accept_response_id_list;
			$transformer 				= new OutgoingAcceptTransformer($accept_response_id_list, $remote_url, 0, 'Outgoing');

	        $data = array(
			    'xml' => $transformer->xmlData
			);
			
			$content = self::curl_post($remote_url, $data, $retcode);
           
			foreach ($accept_response_id_list as $accept_response_id):
			
				$outgoing_accept_response_host_factory 	= OutgoingAcceptResponseHostFactory::get_instance();
				$params = array();
				$params["OutgoingAcceptResponseID"] 	= $accept_response_id;
				$accept_response_host 					= $outgoing_accept_response_host_factory->get_row($params);
	            
				if ($accept_response_host != null):
					$accept_response_host->HttpResponseCode = $retcode;
					$accept_response_host->save();
				endif;
				
			endforeach;
			
            if ($retcode != 200):
            	throw new Exception("REST Call to remote server failed");
            endif;
            
            $transformer = new IncomingAcceptedTransformer($content, 'Dadf.accepted-' . $dadf_version . '.xsd', 'Incoming');
     		
           	header("Content-type: text/xml");
			echo $content;
			
    		
    	} catch (Exception $e) {
    				
    		if (DEBUG === true):
    			echo 'Caught exception: ',  $e->getMessage(), "\n";
    		endif;
    		
    		echo "Accept Response";
    	}    
	}
	
	private static function curl_post($remote_url, $data, &$http_status_code) {
			$request_seconds_timeout = 30;
			
			$ch = curl_init();
			
			//set the url, number of POST vars, POST data
	        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
	        curl_setopt($ch, CURLOPT_URL, $remote_url);
	        curl_setopt($ch, CURLOPT_POST, true);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
	        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
	        curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
	        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $request_seconds_timeout );
	        curl_setopt( $ch, CURLOPT_TIMEOUT, $request_seconds_timeout );
	        
    		$content = curl_exec( $ch );
            $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            
            return $content;
	        
	}
	
}